package cn.linweijian.kafka.producerdemo.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Description: kafkademo
 * <p>
 * Created by linweijian on 2018/5/31 10:34
 */
@Controller
public class SendController {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private KafkaTemplate kafkaTemplate;

    @RequestMapping(value = "/send", method = RequestMethod.GET)
    public ResponseEntity<?> sendKafka(String msg) {
        try {
            logger.info("kafka的消息={}", msg);
            kafkaTemplate.send("test", "key", msg);
            logger.info("发送kafka成功.");
            return ResponseEntity.ok("发送成功");
        } catch (Exception e) {
            logger.error("发送kafka失败", e);
            return new ResponseEntity<>(e,HttpStatus.BAD_REQUEST);
        }
    }
}
